package handlers

import (
	"strconv"
	"sysu-giep/internal/api/middleware"
	"sysu-giep/pkg/logger"
	"sysu-giep/pkg/response"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// GetMatches 获取匹配列表
// @Summary 获取匹配列表
// @Description 获取匹配列表，支持分页和筛选
// @Tags 匹配
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param page query int false "页码" default(1)
// @Param size query int false "每页大小" default(10)
// @Success 200 {object} response.PageResponse{data=[]map[string]interface{}}
// @Router /matches [get]
func GetMatches(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))

	// TODO: 从数据库获取匹配列表
	logger.Info("获取匹配列表", logger.Int("page", page), logger.Int("size", size))

	matches := []map[string]interface{}{
		{
			"id":           1,
			"match_score":  85.5,
			"is_confirmed": false,
			"project": gin.H{
				"id":    1,
				"title": "人工智能在医疗诊断中的应用研究",
			},
			"user": gin.H{
				"id":   1,
				"name": "张三",
			},
			"matched_at": "2024-01-01",
		},
	}

	response.PageSuccess(c, matches, 30, page, size)
}

// ConfirmMatch 确认匹配
// @Summary 确认匹配
// @Description 确认匹配结果
// @Tags 匹配
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "匹配ID"
// @Success 200 {object} response.Response
// @Failure 400 {object} response.Response
// @Router /matches/{id}/confirm [post]
func ConfirmMatch(c *gin.Context) {
	userID := middleware.GetCurrentUserID(c)
	idStr := c.Param("id")

	// TODO: 确认匹配
	logger.Info("确认匹配", zap.Uint64("user_id", uint64(userID)), logger.String("match_id", idStr))

	response.SuccessWithMessage(c, "匹配确认成功", nil)
}

// GetMyMatches 获取我的匹配
// @Summary 获取我的匹配
// @Description 获取当前用户的匹配列表
// @Tags 匹配
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param page query int false "页码" default(1)
// @Param size query int false "每页大小" default(10)
// @Success 200 {object} response.PageResponse{data=[]map[string]interface{}}
// @Router /matches/my [get]
func GetMyMatches(c *gin.Context) {
	userID := middleware.GetCurrentUserID(c)
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))

	// TODO: 获取我的匹配列表
	logger.Info("获取我的匹配", zap.Uint64("user_id", uint64(userID)), logger.Int("page", page), logger.Int("size", size))

	matches := []map[string]interface{}{
		{
			"id":           1,
			"match_score":  85.5,
			"is_confirmed": false,
			"project": gin.H{
				"id":    1,
				"title": "人工智能在医疗诊断中的应用研究",
			},
			"matched_at": "2024-01-01",
		},
	}

	response.PageSuccess(c, matches, 10, page, size)
}
